package com.kaifamiao.advice;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

import java.util.Arrays;

/**
 * @author lucien-ma
 * @date 2021/8/26
 * @apiNote
 */
public class LogAdvice {

    public void before(JoinPoint joinPoint) {
        // 类名
        System.out.println(joinPoint.getTarget().getClass().getName());
        // 方法名
        System.out.println(joinPoint.getSignature().getName());
        // 参数列表
        System.out.println(Arrays.toString(joinPoint.getArgs()));
        System.out.println("开始前执行了");
    }

    public void afterReturnig() {
        System.out.println("正常结束之后执行");
    }

    public void throwException() {
        System.out.println("抛出异常时执行");
    }

    public void after() {
        System.out.println("最后执行");
    }

    public Object around(ProceedingJoinPoint joinPoint) {
        System.out.println("环绕通知");
        Object value = null;
        try {
            System.out.println("方法开始执行.....");
            Object[] args = joinPoint.getArgs();
            // 执行方法
            value = joinPoint.proceed(args);
            System.out.println("方法返回了....");
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            System.err.println("发生了异常...");
        } finally {
            System.out.println("结束执行");
        }
        return value;
    }
}
